package com.adcc.eoss.efb.ngctrl.Service.impl;

import com.adcc.eoss.efb.ngctrl.Service.ICmdService;
import com.adcc.eoss.efb.ngctrl.model.Nginx;
import com.adcc.eoss.efb.ngctrl.util.CmdRunner;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LinuxCmdService implements ICmdService{

    @Autowired
    CmdRunner cmdRunner;

    @Override
    public void stop(String pid) throws IOException {
        for(String s :cmdRunner.runPipeCmd("kill -QUIT "+pid))
        {
            System.out.println(s);
        }
    }

    @Override
    public String getConf(String pid) throws IOException {
        String res = "";
        for (String ngstr: cmdRunner.runPipeCmd("ps -o pid,command -e  |grep "+pid)) {
            if (ngstr.indexOf("nginx: master process") > 0) {
                int c = ngstr.indexOf("-c");
                res = ngstr.substring(c+3).trim();
            }
        }
        return res;
    }

    @Override
    public String getNgPath(String pid) throws IOException {
        String res = "";
        for (String ngstr: cmdRunner.runPipeCmd("ps -o pid,command -e  |grep "+pid)) {
            if (ngstr.indexOf("nginx: master process") > 0) {
                int p = ngstr.indexOf("-p");
                int c = ngstr.indexOf("-c");
                res = ngstr.substring(p+3,c-1);
            }
        }
        return res;
    }

    @Override
    public void start(String path, String conf) throws IOException {
        String cmd = "/usr/local/openresty/nginx/sbin/nginx -p " + path + " -c "+conf ;
        for(String s :cmdRunner.runPipeCmd(cmd))
        {
            System.out.println(s);
        }
    }

    @Override
    public String examConf(String path, String conf) throws IOException {
        String res = "";
        String cmd = "/usr/local/openresty/nginx/sbin/nginx -p " + path + " -c "+conf + " -t";;
        for(String ngstr :cmdRunner.runPipeCmdErrorOutput(cmd))
        {
             res += ngstr;
        }
        if(res != "" && res.indexOf("test is successful") > 0){
            res = "success";
        }
        return res;
    }

    @Override
    public String getConfFilePath(String pid) throws IOException {
        String res = "";
        for (String ngstr: cmdRunner.runPipeCmd("ps -o pid,command -e  |grep "+pid)) {
            if (ngstr.indexOf("nginx: master process") > 0) {
                int p = ngstr.indexOf("-p");
                int c = ngstr.indexOf("-c");
                String pStr = ngstr.substring(p+3,c-1);
                String cStr = ngstr.substring(c+3).trim();
                if(!cStr.startsWith("/")) cStr = "/" + cStr;
                res = pStr+cStr;
            }
        }
        return res;
    }

    @Override
    public List<Nginx> getNgList() {
        List<Nginx> res = new ArrayList<>();
        try {
            for (String ngstr: cmdRunner.runPipeCmd("ps -o pid,command -e  |grep nginx")) {
                if(ngstr.indexOf("nginx: master process") > 0)
                {
                    Nginx ng = new Nginx();
                    Pattern patternPid = Pattern.compile("\\s\\d+\\s");
                    Matcher m = patternPid.matcher(ngstr);
                    if(m.find())
                    {
                        ng.setPid(m.group().toString().trim());
                    }
                    for(String portstr: cmdRunner.runPipeCmd("netstat -a -n -p |grep " + ng.getPid()))
                    {
                        Pattern patternPort = Pattern.compile("0.0.0.0.0:\\d+");
                        Matcher mport = patternPort.matcher(portstr);
                        if(mport.find())
                        {
                            String t = mport.group();
                            ng.setPort(ng.getPort()+" "+mport.group().split(":")[1]);
                        }
                    }
                    ng.setPath(ngstr.split("nginx:")[1]);
                    res.add(ng);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }
}
